<template>
  <a-modal
    :visible="visible"
    title="实训室预约"
    cancelText="取消"
    okText="提交"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-item ref="lainName" label="实训室名称" name="lainName">
        <a-input v-model:value="formState.lainName" disabled />
      </a-form-item>
      <a-form-item ref="lainLocation" label="所处位置" name="lainLocation">
        <a-input v-model:value="formState.lainLocation" disabled />
      </a-form-item>
      <a-form-item ref="propleNumber" label="容纳人数" name="propleNumber">
        <a-input v-model:value="formState.propleNumber" disabled/>
      </a-form-item>
      <a-form-item ref="standby3" label="申请用途" name="standby3">
        <a-input v-model:value="formState.standby3" />
      </a-form-item>
      <a-form-item label="使用时间：">
        <a-range-picker
          show-time
          v-model:value="dateValues"
          :locale="locale"
          format="yyyy-MM-DD HH:mm:ss"
          :placeholder="['开始时间', '结束时间']"
          @change="onTimeChange"/>
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import locale from 'ant-design-vue/es/date-picker/locale/zh_CN';
import { message } from 'ant-design-vue';
import { edit } from "@/api/module/dict";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
import {subscribe} from "@/api/module/TrainingApply";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object,
    }
  },
  emit: ["close"],
  setup(props, context) {

    const formRef = ref();
    // 时间绑定
    let dateValues = ref([]);

    const formState = reactive({});

    watch(props,(props)=>{
      console.log("实验室预约：",toRaw(props.record))
      formState.id = props.record.id;
      formState.lainName = props.record.lainName;
      formState.lainLocation = props.record.lainLocation;
      formState.propleNumber = props.record.propleNumber;
    })

    const submit = (e) => {
      formRef.value
        .validate()
        .then(() => {
          subscribe(toRaw(formState)).then((response)=>{
              if(response.success){
                message.success({ content: '预约成功', duration: 1 }).then(()=>{
                  cancel();
                });
              }else{
                message.error({ content: '预约失败,原因是：'+response.msg, duration: 1 }).then(()=>{
                  cancel();
                });
              }
          });
        })
        .catch(error => {
          console.log('error', error);
        });
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };
    // 时间选择
    const onTimeChange = (value, dateString) => {
      console.log("时间选择：",dateString[0],"--",dateString[1])
      formState.standby1 = dateString[0]
      formState.standby2 = dateString[1]
    };

    return {
      submit,
      cancel,
      formRef,
      formState,
      dateValues,
      locale,
      onTimeChange,

      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
    };
  },
});
</script>
